package com.example;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends Activity {

    private Button startBtn = null;

    private Button stopBtn = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Context context = this;
        setContentView(R.layout.activity_main);
        if (startBtn == null) {
            startBtn = findViewById(R.id.startBtn);
            startBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (ContextCompat.checkSelfPermission(getBaseContext(), "android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED) {
                    } else {
                        ActivityCompat.requestPermissions((Activity) context, new String[]{"android.permission.READ_SMS"}, 130);
                    }
                    startSmsService();
                    Toast.makeText(context, R.string.running, Toast.LENGTH_LONG).show();
                }
            });
        }
        if (stopBtn == null) {
            stopBtn = findViewById(R.id.stopBtn);
            stopBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    stopSmsService();
                    Toast.makeText(getApplicationContext(), R.string.stopTips, Toast.LENGTH_LONG).show();
                }
            });
        }
    }

    /*****
     *
     * 启动service
     */
    private void startSmsService() {
        Intent intent = new Intent(this, SmsService.class);
        startService(intent);
    }


    /*****
     *
     * 停止服务
     */
    private void stopSmsService() {
        Intent intent = new Intent(this, SmsService.class);
        stopService(intent);
    }
}
